Shader "Depth"
{
    Properties
    {
        _Depth("Depth", Float) = 0.5
    }
    SubShader
    {
        Tags
        {
            "RenderPipeline"="UniversalPipeline"
            "LightMode"="UniversalForward"
            "Queue"="Transparent"
        }
        LOD 100

        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off
        Cull Back

        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
        
        CBUFFER_START(UnityPerMaterial)
            half _Depth;
        CBUFFER_END
        ENDHLSL

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct a2v
            {
                float3 positionOS   : POSITION;
            };

            struct v2f
            {
                float4 positionCS   : SV_POSITION;
                float3 positionWS   : TEXCOORD0;
                float3 positionVS   : TEXCOORD1;
            };


            v2f vert(a2v input)
            {
                v2f output;

                VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
                output.positionCS = vertexInput.positionCS;
                output.positionWS = vertexInput.positionWS;
                output.positionVS = vertexInput.positionVS;
                return output;
            }

            half4 frag(v2f input) : SV_TARGET
            {
                half4 screenPosition = ComputeScreenPos(input.positionCS);
                half2 pixelPosition = float2(input.positionCS.x,(_ProjectionParams.x < 0)? (_ScaledScreenParams.y - input.positionCS.y) : input.positionCS.y);
                half2 positionNDC = pixelPosition.xy / _ScaledScreenParams.xy;
                positionNDC.y = 1.0f - positionNDC.y;

                half sceneRawDepth = lerp(LinearEyeDepth(SampleSceneDepth(positionNDC.xy),_ZBufferParams),SampleSceneDepth(positionNDC.xy),unity_OrthoParams.w);
                half3 viewVector = (-1* (_WorldSpaceCameraPos.xyz - input.positionWS))/ screenPosition.w;
                
                //重建透视
                half3 perspectiveCode = viewVector * sceneRawDepth.xxx + _WorldSpaceCameraPos;
                
                //重建正交
                half worldUnit = -1* lerp(_ProjectionParams.y, _ProjectionParams.z, 1 - SampleSceneDepth(positionNDC.xy));
                half3 orthogonalCode = mul(UNITY_MATRIX_I_V, half4(input.positionVS.x, input.positionVS.y, worldUnit, 1)).xyz;

                //世界坐标
                half3 worldPos = lerp(perspectiveCode, orthogonalCode, (unity_OrthoParams.w));
                worldPos = input.positionWS - worldPos;

                half depth = saturate(worldPos.y * _Depth);
                return  half4(depth.xxx,1);
            }
            ENDHLSL
        }
    }
}